using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chimera2;
using Chimera2.Classes;

namespace Chimera2.GUI
{
    public partial class Hubs : Form
    {
        Hub hub = new Hub();

        // define delegate
        public delegate void PassControl(object sender);

        // create instance (null)
        public PassControl passControl;

        public Hubs()
        {
        }

        public Hubs(Hub _hub)
        {
            InitializeComponent();
            hub = _hub;
            OnLoad();
        }

        private void chkOverrideBotName_CheckedChanged(object sender, EventArgs e)
        {
            txtOverrideBotName.Enabled = ((CheckBox)sender).Checked;
        }

        private void OnLoad()
        {
            txtHubName.Text = hub.HubName;
            txtHubAddress.Text = hub.HubAddress;
            txtHubcommandName.Text = hub.HubBotName;
            chkOverrideBotName.Checked = hub.OverrideBotName;
            txtOverrideBotName.Enabled = hub.OverrideBotName;
            txtOverrideBotName.Text = hub.BotName;
            txtBotPassword.Text = hub.BotPassword;
        }

        private void OnSave()
        {
            hub.HubName = txtHubName.Text;
            hub.HubAddress = txtHubAddress.Text;
            hub.HubBotName = txtHubcommandName.Text;
            hub.OverrideBotName = chkOverrideBotName.Checked;
            hub.BotName = txtOverrideBotName.Text;
            hub.BotPassword = txtBotPassword.Text;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHubName.Text) || 
                string.IsNullOrEmpty(txtHubAddress.Text) ||
                string.IsNullOrEmpty(txtHubcommandName.Text))
            {
                MessageBox.Show("'Hub name', 'Hub address' and 'Hub command-bot name' is required.", "Hub", MessageBoxButtons.OK);
            }
            else
            {
                OnSave();
                passControl(hub);
                this.Close();
            }
        }
    }
}